﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:BAM="http://schemas.microsoft.com/BizTalkServer/2004/10/BAM"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

  <xsl:param name="ServiceName" />
  <xsl:param name="ActivityName" />
  <xsl:param name="NamespaceName" />
  
  <xsl:template match="BAM:BAMDefinition">
    <xsl:variable name="ActivityIdName"><xsl:value-of select="$ServiceName" />ActivityID</xsl:variable>

    using System;
    using System.Configuration;
    using Microsoft.BizTalk.Bam.EventObservation;

    namespace <xsl:value-of select="$NamespaceName" />
    {
      public class <xsl:value-of select="$ServiceName" /> : I<xsl:value-of select="$ServiceName" />
      {
        public void LogCompleteActivity(string activityId, <xsl:value-of select="$ServiceName" />Type value)
        {
          EventStream es = CreateEventStream();

          es.BeginActivity("<xsl:value-of select="$ActivityName" />", activityId);

          es.UpdateActivity("<xsl:value-of select="$ActivityName" />", activityId, <xsl:apply-templates select="BAM:Activity[@Name = $ActivityName]/BAM:Checkpoint" />);
          
          es.EndActivity("<xsl:value-of select="$ActivityName" />", activityId);
        }
        
        public void BeginActivity(string activityId)
        {
          EventStream es = CreateEventStream();
          
          es.BeginActivity("<xsl:value-of select="$ActivityName" />", activityId);
        }

        public void UpdateActivity(string activityId, <xsl:value-of select="$ServiceName" />Type value)
        {
          EventStream es = CreateEventStream();
          
          es.UpdateActivity("<xsl:value-of select="$ActivityName" />", activityId, <xsl:apply-templates select="BAM:Activity[@Name = $ActivityName]/BAM:Checkpoint" />);
        }

        public void EnableContinuation(string activityId, string continuationToken)
        {
          EventStream es = CreateEventStream();
          
          es.EnableContinuation("<xsl:value-of select="$ActivityName" />", activityId, continuationToken);
        }

        public void AddActitivityReference(string activityId, string referenceActivityName, string referenceActivityId)
        {
          EventStream es = CreateEventStream();
          
          es.AddRelatedActivity("<xsl:value-of select="$ActivityName" />", activityId, referenceActivityName, referenceActivityId);
        }

        public void AddReference(string activityId, string referenceType, string referenceName, string referenceData)
        {
          EventStream es = CreateEventStream();
          
          es.AddReference("<xsl:value-of select="$ActivityName" />", activityId, referenceType, referenceName, referenceData);
        }

        public void AddLongReference(string activityId, string referenceType, string referenceName, string referenceData, string referenceLongData)
        {
          EventStream es = CreateEventStream();
          
          es.AddReference("<xsl:value-of select="$ActivityName" />", activityId, referenceType, referenceName, referenceData, referenceLongData);
        }

        public void EndActivity(string activityId)
        {
          EventStream es = CreateEventStream();
          
          es.EndActivity("<xsl:value-of select="$ActivityName" />", activityId);
        }

        private EventStream CreateEventStream()
        {
          return new DirectEventStream(ConfigurationManager.ConnectionStrings["BizTalkBAMPrimaryDb"].ConnectionString, 1);
        }
      }
    }
  </xsl:template>

  <xsl:template match="BAM:Checkpoint">
    "<xsl:value-of select="@Name" />", value.<xsl:value-of select="@Name" /><xsl:if test="position()!=last()">,</xsl:if>
  </xsl:template>
</xsl:stylesheet>
